package com.army.study;

import com.army.study.slice.MainAbilitySlice;
import com.army.study.utils.LogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability implements IAbilityContinuation {
    private static final String TAG = MainAbility.class.getSimpleName();
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
    }

    private void requestPermission() {
        String[] permissions = {
                "ohos.permission.READ_USER_STORAGE",
                "ohos.permission.WRITE_USER_STORAGE",
                "ohos.permission.DISTRIBUTED_DATASYNC"
        };

        List<String> applyPermissions = new ArrayList<>();
        for (String element : permissions) {
            LogUtil.info(TAG, "check permission: " + element);
            checkPermission(applyPermissions, element);
        }
        requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0);
    }

    private void checkPermission(List<String> applyPermissions, String element) {
        if (verifySelfPermission(element) != 0) {
            if (canRequestPermission(element)) {
                applyPermissions.add(element);
            }else {
                LogUtil.info(TAG, "user deny permission");
            }
        }else {
            LogUtil.info(TAG, "user granted permission: " + element);
        }
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
}
